package com.dlc.hardwareprotocol.serialport.utils;

import java.io.UnsupportedEncodingException;

public class CRC16Utils {
    public CRC16Utils() {
    }

    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = null;

        try {
            bs = str.getBytes("gb2312");
        } catch (UnsupportedEncodingException var6) {
            var6.printStackTrace();
        }

        for(int i = 0; i < bs.length; ++i) {
            int bit = (bs[i] & 240) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 15;
            sb.append(chars[bit]);
        }

        return sb.toString();
    }

    public static byte[] stringToHexByte(String hex) {
        int len = hex.length() / 2;
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();

        for(int i = 0; i < len; ++i) {
            int pos = i * 2;
            result[i] = (byte)(toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }

        return result;
    }

    private static int toByte(char c) {
        byte b = (byte)"0123456789ABCDEF".indexOf(c);
        return b;
    }

    public static byte[] HexString2Bytes(String src) {
        if (null != src && 0 != src.length()) {
            byte[] ret = new byte[src.length() / 2];
            byte[] tmp = src.getBytes();

            for(int i = 0; i < tmp.length / 2; ++i) {
                ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
            }

            return ret;
        } else {
            return null;
        }
    }

    public static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0}));
        _b0 = (byte)(_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1}));
        byte ret = (byte)(_b0 ^ _b1);
        return ret;
    }

    public static String getCRC16(String source) {
        int crc = 41452;
        int polynomial = 4129;
        byte[] bytes = stringToHexByte(source);
        byte[] var4 = bytes;
        int var5 = bytes.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            byte b = var4[var6];

            for(int i = 0; i < 8; ++i) {
                boolean bit = (b >> 7 - i & 1) == 1;
                boolean c15 = (crc >> 15 & 1) == 1;
                crc <<= 1;
                if (c15 ^ bit) {
                    crc ^= polynomial;
                }
            }
        }

        crc &= 65535;
        StringBuilder result = new StringBuilder(Integer.toHexString(crc));

        while(result.length() < 4) {
            result.insert(0, "0");
        }

        return result.toString().toUpperCase();
    }

    public static String getCRC16(byte[] source) {
        int crc = 41452;
        int polynomial = 4129;
        byte[] var3 = source;
        int var4 = source.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            byte b = var3[var5];

            for(int i = 0; i < 8; ++i) {
                boolean bit = (b >> 7 - i & 1) == 1;
                boolean c15 = (crc >> 15 & 1) == 1;
                crc <<= 1;
                if (c15 ^ bit) {
                    crc ^= polynomial;
                }
            }
        }

        crc &= 65535;
        StringBuilder result = new StringBuilder(Integer.toHexString(crc));

        while(result.length() < 4) {
            result.insert(0, "0");
        }

        return result.toString().toUpperCase();
    }

    public static String getCRC(byte[] bytes) {
        byte[] mbyte = new byte[1];

        for(int i = 0; i < bytes.length - 1; ++i) {
            if (i == 0) {
                mbyte[0] = (byte)(bytes[i] ^ bytes[i + 1]);
            } else {
                mbyte[0] ^= bytes[i + 1];
            }
        }

        return ByteUtil.bytes2HexStr(mbyte);
    }
}
